<?php

class Admin_Table_Categories extends Apk_Db_Table
{
	protected $_name      = 'categories';
    protected $_primary   = 'id';

    /*protected $_dependentTables = array('Admin_Table_Rule', 'Admin_Table_Categories', 'Admin_Table_Privilege');

	protected $_referenceMap = array('Parent' => array(
            								            'columns'       => 'parent_id',
            								            'refTableClass' => 'Admin_Table_Categories',
            								            'refColumns'    => 'id'));*/
    public $_ERROR = '';
    public $_lang = 'ru';

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetResources()
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('is_active')
    				->order('l');

    	return $this->getList($sel)->toArray();
    }


    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function GetDataList()
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)
    		  ->joinLeft('categories_menus', 'categories_menus.id = categories.id_menu', array('menu' => 'name'))
    		  ->order('l');


    	$data['data'] = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	return $data;
    }

 	public function GetChildList($id, $a = array(), $params = null, $role = null)
    {
    	$p = $this->GetDataRow($id);

    	$this->_acl = Acl::getInstance()->getAcl();
    	
    	//print_r($this->_acl);die();

    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title'=>'title_'.$this->_lang, 'menu_name'=>'menu_name_'.$this->_lang, 'level', 'url', 'parent_id', 'l', 'r', 'is_show', 'site','controller', 'module', 'idt'))
    		  ->where('is_active')
    		  ->where('l > ?', $p['l'])
    		  ->where('r < ?', $p['r'])
    		  ->order('l');


    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}


    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	if(count($a) > 0 && is_array($a))
    	foreach ($data as $k => $v)
    	{
    		if(in_array($v['id'], $a)) $data[$k]['__attributes']['active'] = 1;

    		if(!$this->_acl->isAllowed($role, $v['site'].':'.$v['module'].':'.$v['controller'], 'index')){
				unset($data[$k]);
    		}
    	}

    	return $data;
    }

    public function GetCatList($a = array(), $params = null)
    {

    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title'=>'title_'.$this->_lang, 'menu_name'=>'menu_name_'.$this->_lang, 'level', 'url', 'parent_id', 'l', 'r', 'is_show'))
    		  ->where('is_active')
    		  ->order('l');

    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}

    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	if(count($a) > 0 && is_array($a))
    	foreach ($data as $k => $v)
    		if(in_array($v['id'], $a)) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetTypesList($a)
    {
    	$sel = $this->getAdapter()->select()
    			->from('categories_types')
    			->order('id');
    	$data = $this->getList($sel)->toArray();

    	if($a > 0)
    	foreach ($data as $k => $v)
    		if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetMenusList($a)
    {
    	$sel = $this->getAdapter()->select()
    			->from('categories_menus')
    			->order('id');
    	$data = $this->getList($sel)->toArray();

    	if($a > 0)
    	foreach ($data as $k => $v)
    		if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

    	return $data;
    }

    public function GetChildsID($id)
    {
    	$p = $this->GetDataRow($id);

    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id'))
    		  ->where('is_active')
    		  ->where('l > ?', $p['l'])
    		  ->where('r < ?', $p['r'])
    		  ->order('l');

    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }

    public function GetCatByType($type, $a = 0,$lang='ru')
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'title'=>'title_'.$lang,
    		  							'menu_name'=>'menu_name_'.$lang,
    		  							'level', 'url', 'parent_id', 'l', 'r', 'is_show'))
    		  ->where('is_active')
    		  ->where('id_type = ?', $type)
    		  ->order('l');

    	$data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

    	if($a > 0){
    		foreach ($data as $k => $v)
    			if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;
    	}

    	return $data;
    }


	public function insert(array $data)
    {
		if(!is_array($data)) $data = array();
        return parent::insert($data);
    }

    public function update(array $data, $id, $user_id = 0)
    {

    	$data['modify_date'] = time();
		$data['modify_user_id'] = $user_id;
		if(!isset($data['is_show_gallery'])) $data['is_show_gallery'] = 0;
		if(!isset($data['is_idt'])) $data['is_idt'] = 0;

		try{
			parent::update($data, 'id = '.$id);
		}catch(Exception $e)
		{
			echo $e->__toString();
			die();
		}

		return true;
    }

    public function update_all(array $data, $id, $user_id = 0)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
		return parent::update($data, 'id = '.$id);
    }

    public function remove($id)
    {
    	parent::delete('id = '.$id);
	}

    public function photo_count($id)
    {
    	$sel = $this->getAdapter()->select()
    					->from('objects', array('count' => 'count(id)'))
    					->where('is_active')
    					->where('is_ghost = 0')
    					->where('is_removed = 0')
    					->where('id_cat = ?', $id);

    	$count = $this->getList($sel)->getRow(0)->toArray();

    	$d['count_photo'] = $count['count'];
    	parent::update($d, 'id = '.$id);
    }

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'add_date':
            case 'modify_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>